import sys

import pygame

from jeo.box import Box
from jeo.brick_manager import BrickManager
from jeo.constants import *


class GameWorld:
    def __init__(self):
        pygame.init()
        self.width = SCREEN_WIDTH
        self.height = SCREEN_HEIGHT
        self.fps = FPS

        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption(GAME_TITLE)

        self.isRunning = True

        self.goes = pygame.sprite.Group()
        self.bricks = pygame.sprite.Group()

        self.add_goes()

        self.brick_manager = BrickManager(self)


    def start(self):
        while self.isRunning:
            # --------------- event  ---------------
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    self.isRunning = False


            # --------------- update  ---------------
            for go in self.goes:
                go.update(events)
            self.brick_manager.update(events)

            # --------------- draw  ---------------
            self.screen.fill("black")

            for go in self.goes:
                go.draw(self.screen)

            pygame.display.flip()

            # --------------- clock  ---------------
            self.clock.tick(self.fps)

        pygame.quit()
        sys.exit()

    def add_goes(self):
        go = Box(self)
        self.goes.add(go)



